# 28. strStr
function strStr(haystack, needle) {
const len = needle.length;
for (let i = 0; i < haystack.length - len + 1; i++) {
const str = haystack.substr(i, len);
if (str === needle) return i;
}
return -1;
}
console.log(strStr("hello", "ll"));
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
← 279. 完全平方数 283. 移动零 →